//
//  DetailShareView.swift
//  LeGou
//
//  Created by lxm on 2022/7/28.
//

import UIKit

class DetailShareView: UIView {
    
    let title = ZMUtil.label(title: "分享给好友购买赚￥1元", color: .white, font: UIFont.systemFont(ofSize: 14, weight: .regular))
    let img = ZMUtil.imageView(imageName: "detail_hb")
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }
    
    func setUI() {
        backgroundColor = UIColor.zm.color(hexString: "#000000", 0.65)
        let stack = ZMUtil.stackView(axis: .horizontal, spacing: 4)
        stack.addArrangedSubview(img)
        stack.addArrangedSubview(title)
        stack.alignment = .center
        stack.distribution = .fill
        addSubview(stack)
        stack.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.top.equalToSuperview().offset(4)
            make.bottom.equalToSuperview().offset(-4)
        }
        
        img.snp.makeConstraints { make in
            make.width.equalTo(32)
            make.height.equalTo(21)
        }
        
        let tan = TriangleView()
        tan.fillColor = UIColor.zm.color(hexString: "#000000", 0.65)
        addSubview(tan)
        tan.snp.makeConstraints { make in
            make.top.equalTo(self.snp.bottom)
            make.height.equalTo(5)
            make.width.equalTo(8)
            make.centerX.equalTo(stack.snp.centerX)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layer.cornerRadius = zm.height / 2
    }
    
    
    class  TriangleView : UIView {
        var fillColor = "#FDDF00".zm.hex()
        
        var direction : UISwipeGestureRecognizer.Direction = .down
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            setUI()
        }
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            setUI()
        }
        
        func setUI() {
            backgroundColor = .clear
        }
        
        override func draw(_ rect: CGRect) {
            super.draw(rect)
            let b = UIBezierPath()
            if direction == .down {
                b.move(to: CGPoint(x: 0, y: 0))
                b.addLine(to: CGPoint(x: rect.size.width, y: 0))
                b.addLine(to: CGPoint(x: rect.size.width / 2, y: rect.size.height))
            } else if direction == .right {
                b.move(to: CGPoint(x: 0, y: 0))
                b.addLine(to: CGPoint(x: rect.size.width, y: rect.size.height / 2))
                b.addLine(to: CGPoint(x: 0, y: rect.size.height))
            } else if direction == .left {
                b.move(to: CGPoint(x: rect.size.width, y: 0))
                b.addLine(to: CGPoint(x: 0, y: rect.size.height / 2))
                b.addLine(to: CGPoint(x: rect.size.width, y: rect.size.height))
            } else if direction == .up {
                b.move(to: CGPoint(x: rect.size.width / 2, y: 0))
                b.addLine(to: CGPoint(x: rect.size.width, y: rect.size.height))
                b.addLine(to: CGPoint(x: 0, y: rect.size.height))
            }
            
            fillColor.setFill()
            b.close()
            b.fill()
        }
    }
}
